import Vue from 'vue'
import VueRouter from 'vue-router'

import { getToken } from '@/utils/storage'

const Home = () => import('@/views/Home.vue')
const Login = () => import('@/views/Login.vue')
const HomeFirst = () => import('@/views/HomeFirst.vue')
const HomeSecond = () => import('@/views/HomeSecond.vue')
const HomeThird = () => import('@/views/HomeThird.vue')
const HomeFourth = () => import('@/views/HomeFourth.vue')
const ArticleIndex = () => import('@/views/ArticleIndex.vue')
const HomeFourthUser = () => import('@/views/HomeFourthUser.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Home,
    children: [
      { path: '/', redirect: '/home-1' },
      { path: '/home-1', component: HomeFirst },
      { path: '/home-2', component: HomeSecond },
      { path: '/home-3', component: HomeThird },
      { path: '/home-4', component: HomeFourth }
    ]
  },
  { path: '/login', component: Login },
  { path: '/article/:id', component: ArticleIndex },
  { path: '/home-4/user', component: HomeFourthUser }
]

const router = new VueRouter({
  routes
})
// 白名单
// const whiteList = ['/login', '/register']
// 导航守卫
router.beforeEach((to, from, next) => {
  // 1. to往哪里去， 到哪去的路由信息对象
  // 2. from 从哪里来， 从哪来的路由信息对象
  // 3. next() 是否放行
  // 如果next()调用，就是放行
  // next(路径) 拦截到某个路径页面
  const token = getToken()
  if (!token) {
    if (to.path === '/home-4') {
      next('/login')
    }
  }
  next()
})

export default router
